﻿//统一文件为utf-8编码编译
#pragma execution_character_set("utf-8")
///////////////////////////////////////////////////////////////////////////
// 文件名:     filelistthread.cpp
// 描述:
// 作者:      颜磊
// 创建工程：   AudioComment
// 创建时间:   2024-06-27 11:11
///////////////////////////////////////////////////////////////////////////
#include "filelistthread.h"
#include <QDir>
#include <QFile>
#include <QDebug>

FileListThread::FileListThread(QObject *parent) : QThread(parent)
{

}

FileListThread::~FileListThread()
{

}

void FileListThread::stop()
{
    isRun = false;
}

void FileListThread::setDirPath(const QString &value)
{
    dirPath = value;
}

void FileListThread::setFilter(const QString &value)
{
    filter = value;
}

void FileListThread::run()
{
    QString disposeDirPath = "";
    QString fileFilter = "";
    QStringList filterList;
    isRun = true;
    while (isRun) {
        if((disposeDirPath != dirPath) || (fileFilter != filter)){
            disposeDirPath = dirPath;
            fileFilter = filter;
            filterList = fileFilter.split(';');
            signal_clearList();
            continue;
        }
        if(QFile::exists(disposeDirPath)){
            QDir dir(dirPath);
            if(filterList.size() > 1){
//                qDebug() << "过滤"<<filterList.size() << filterList;
                dir.setNameFilters(filterList);
            }
            QFileInfoList  childFiles = dir.entryInfoList(QDir::Files);
            QList<YFileListInfo> list;
            YFileListInfo yFileListInfo;
            for(QFileInfo &info:childFiles){
                yFileListInfo.marked = (QFile::exists(QString("%1/%2.json").arg(info.absolutePath()).arg(info.completeBaseName())));
                yFileListInfo.setFileName(info.fileName().toStdString());
                yFileListInfo.setFilePath(info.filePath().toStdString());
                list.append(yFileListInfo);
            }
            signal_fileList(list);
        }
        QThread::msleep(2000);
    }
}
